/**
 * Copyright &copy; 2012-2016 Civet All rights reserved.
 */
package com.civet.myadmin.modules.enviroment.enterpriseController;

import com.civet.myadmin.common.config.Global;
import com.civet.myadmin.common.utils.StringUtils;
import com.civet.myadmin.common.web.BaseController;
import com.civet.myadmin.modules.cms.entity.Article;
import com.civet.myadmin.modules.cms.entity.Category;
import com.civet.myadmin.modules.cms.entity.Site;
import com.civet.myadmin.modules.cms.service.CategoryService;
import com.civet.myadmin.modules.cms.service.FileTplService;
import com.civet.myadmin.modules.cms.service.SiteService;
import com.civet.myadmin.modules.cms.utils.TplUtils;
import com.civet.myadmin.modules.enviroment.entity.Enterprise;
import com.google.common.collect.Lists;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 栏目Controller
 * @author PlamCivet
 * @version 2013-4-21
 */
@Controller("audditController")
@RequestMapping(value = "${adminPath}/enterprise/audit")
public class AuditController extends BaseController {

	@Autowired
	private CategoryService categoryService;
    @Autowired
   	private FileTplService fileTplService;
    @Autowired
   	private SiteService siteService;

//	@ModelAttribute("audit")
//	public Enterprise get(@RequestParam(required=false) Integer id) {
//		if (id != null){
//			return getEnterprise(id);
//		}else{
//			return new Enterprise();
//		}
//	}

	@RequestMapping(value = {"list", ""})
	public String list(Model model) {
		List<Enterprise> list = Lists.newArrayList();
		fakerList(list);
		model.addAttribute("list", list);
		return "modules/enterprise/auditList";
	}

	public static void fakerList(List<Enterprise> list) {
		for(int i=0;i<10;i++){
			Enterprise enterprise = getEnterprise(i);
			list.add(enterprise);
		}
	}

	@RequestMapping(value = "form")
	public String form(Category category, Model model,String id) {
		Enterprise enterprise = new Enterprise();
		if(id!=null&!"".equals(id)) {
			enterprise = getEnterprise(Integer.parseInt(id));
		}
        model.addAttribute("listViewList",getTplContent(Category.DEFAULT_TEMPLATE));
        model.addAttribute("audit_DEFAULT_TEMPLATE",Category.DEFAULT_TEMPLATE);
        model.addAttribute("contentViewList",getTplContent(Article.DEFAULT_TEMPLATE));
        model.addAttribute("article_DEFAULT_TEMPLATE",Article.DEFAULT_TEMPLATE);
		model.addAttribute("audit", enterprise);
		return "modules/enterprise/auditForm";
	}

	public static Enterprise getEnterprise(int id) {
		Enterprise enterprise = new Enterprise();
		enterprise.setLocation("上海");
		enterprise.setAddress("广东");
		enterprise.setAuditorAndAuditTime("1号，2018-01-01");
		enterprise.setAuditTimeAndId("2018-01-01,1");
		enterprise.setCheckTimeAndId("2100-01-01,1");
		enterprise.setCleanAuditTime(new Date());
		enterprise.setCoverArea("地球");
		enterprise.setCreditCode("998");
		enterprise.setDischargePermitId("666");
		enterprise.setDischargeValidTime(new Date());
		enterprise.setEnterpriseName("宠物小精灵心灵供电所");
		enterprise.setEnvRepresentative("小智");
		enterprise.setEnvRepresentativePhone("987654321");
		enterprise.setId(id);
		enterprise.setItemCategory("公益");
		enterprise.setItemName("用爱发电");
		enterprise.setMonitoringFacilities("电");
		enterprise.setProductionInput("十亿伏特");
		enterprise.setProductionTime(new Date());
		enterprise.setRepresentative("皮卡丘");
		enterprise.setRepresentativePhone("123456789");
		enterprise.setStandbySolution("备用方案");
		Map map = new HashMap();
		map.put("OX", "1");
		map.put("AN", "2");
		map.put("SO2", "3");
		map.put("NO", "4");
		enterprise.setTotal(map);
		return enterprise;
	}

	@RequestMapping(value = "save")
	public String save(Category category, Model model, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + adminPath + "/enterprise/audit/";
		}
		if (!beanValidator(model, category)){
			return form(category, model,"");
		}
		categoryService.save(category);
		addMessage(redirectAttributes, "保存栏目'" + category.getName() + "'成功");
		return "redirect:" + adminPath + "/enterprise/audit/";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Category category, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + adminPath + "/enterprise/audit/";
		}
		if (Category.isRoot(category.getId())){
			addMessage(redirectAttributes, "删除栏目失败, 不允许删除顶级栏目或编号为空");
		}else{
			categoryService.delete(category);
			addMessage(redirectAttributes, "删除栏目成功");
		}
		return "redirect:" + adminPath + "/enterprise/audit/";
	}

	/**
	 * 批量修改栏目排序
	 */
	@RequestMapping(value = "updateSort")
	public String updateSort(String[] ids, Integer[] sorts, RedirectAttributes redirectAttributes) {
    	int len = ids.length;
    	Category[] entitys = new Category[len];
    	for (int i = 0; i < len; i++) {
    		entitys[i] = categoryService.get(StringUtils.toInteger(ids[i]));
    		entitys[i].setSort(sorts[i]);
    		categoryService.save(entitys[i]);
    	}
    	addMessage(redirectAttributes, "保存栏目排序成功!");
		return "redirect:" + adminPath + "/enterprise/audit/";
	}
	
	@RequiresUser
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(String module, @RequestParam(required=false) String extId, HttpServletResponse response) {
		response.setContentType("application/json; charset=UTF-8");
		List<Map<String, Object>> mapList = Lists.newArrayList();
		return mapList;
	}

    private List<String> getTplContent(String prefix) {
   		List<String> tplList = fileTplService.getNameListByPrefix(siteService.get(Site.getCurrentSiteId()).getSolutionPath());
   		tplList = TplUtils.tplTrim(tplList, prefix, "");
   		return tplList;
   	}
}
